﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using AndroidEditor.Compiler;
using AndroidEditor.Unit;
using System.IO;
using System.Collections;
namespace AndroidEditor.SettingClass
{
    class 空间设置
    {
        private bool 是否保存 = false;
        private int aVersion = -1;
        private List<ProjectSetting> settingVecot = new List<ProjectSetting>();
        private bool arm = true;
        private bool armv7 = false;
        private string sysroot = "";
        private string FilePath = "";
        private bool SaveFile = false;
        private XmlDocument xmldoc = new XmlDocument();

        public 空间设置()
        {

        }
        public void Remove(string Name)
        {
            foreach (ProjectSetting setting in settingVecot)
            {
                if (Name == setting.getProjectName())
                {
                    settingVecot.Remove(setting);
                    return;
                }
            }
        }
        public ProjectSetting AddProjectSetting(string name)
        {
            foreach (ProjectSetting setting in settingVecot)
            {
                if (name == setting.getProjectName())
                {
                    return null;
                }
            }
            ProjectSetting nsetting = new ProjectSetting(name);

            settingVecot.Add(nsetting);
            return nsetting;
        }

        public int GetPlatForm()
        {
            return aVersion;

        }

        public void SetPlatForm(int value)
        {
            aVersion = value;
        }



        public bool isarmeabv7()
        {
            return armv7;
        }

        public void SetCpu(bool arm, bool armv7)
        {
            this.arm = arm;
            this.armv7 = armv7;
        }

        public bool isarmeab()
        {
            return arm;
        }




        public CompilerEr Compile()
        {
            foreach (ProjectSetting settingItem in settingVecot)
            {
                HeadSetting hsetting = settingItem.GetHeadSetting();
                SourceSetting ssetting = settingItem.GetSourceSetting();


                List<FileSetting> allitem = ssetting.GetItem();
                foreach (FileSetting file in allitem)
                {
                    string fileitem = file.GetFile();
                    string compiler = GetProjectCompiler(settingItem.getCtype());
                    sysroot = GlobleSetting.Instance().GetNDK() + @"\platforms\" + @"android-" + GetPlatForm().ToString() + @"\arch-arm";
                    sysroot = sysroot.Replace(@"\", "/");
                    string sysrootStr = "--sysroot=" + sysroot;
                    CompilerEr er = new CompilerEr();


                    er.SetCompier(compiler);
                    er.SetPlatFormFlag("-DANDROID=" + GetPlatForm().ToString());
                    er.SetFile(fileitem);
                    er.SetRootSys(sysrootStr);
                    er.SetDependLibs(settingItem.GetDependLibsDirectory());
                    er.SetDependHead(settingItem.GetDependHeaderDirectory());
                    er.SetProject(settingItem);
                    string outfile = er.Start();

                    if (er.ErrorCode > 0)
                    {
                        return er;
                    }


                }

            }
            return null;
        }

        private string GetProjectCompiler(NprojectType.CType cType)
        {
            if (cType == NprojectType.CType.C)
            {
                return GlobleSetting.Instance().GetCompilerDIR() + @"\arm-linux-androideabi-gcc.exe";
            }

            return GlobleSetting.Instance().GetCompilerDIR() + @"\arm-linux-androideabi-g++.exe";

        }

        public bool isSaved()
        {
            return 是否保存;
        }

        public void SetSave()
        {
            是否保存 = true;
        }

        public bool isSavedToFile()
        {
            return SaveFile;
        }

        public void SetSavedToFile()
        {
            SaveFile = true;
        }



        public void SetTagPath(string path)
        {
            FilePath = path;


        }

        public void SaveXml()
        {
            XmlNode root = Unit.XmlUnit.GetRoot(xmldoc);
            XmlNode workspace = XmlUnit.FindNodeOrCreateNew(root, "WorkSpace");
            XmlAttribute attribute = XmlUnit.FindAttributeOrCreateNew(workspace, "Name");
            attribute.Value = Path.GetDirectoryName(FilePath);
            XmlAttribute version = XmlUnit.FindAttributeOrCreateNew(workspace, "AndroidVersion");
            version.Value = Convert.ToString(aVersion);

            XmlAttribute armatt = XmlUnit.FindAttributeOrCreateNew(workspace, "Arm");
            armatt.Value = Convert.ToString(arm);
            XmlAttribute armv7att = XmlUnit.FindAttributeOrCreateNew(workspace, "Armv7");
            armv7att.Value = Convert.ToString(armv7);
            foreach (ProjectSetting settingItem in settingVecot)
            {
                XmlNode node = XmlUnit.FindOrCreateNewNodeWithValue(root, "PSetting", "Name", settingItem.getProjectName());
                SaveSetting(node, settingItem);
            }

            xmldoc.Save(FilePath + @"\" + Path.GetFileName(FilePath) + ".ae");


        }

        private void SaveSetting(XmlNode node, ProjectSetting settingItem)
        {
            XmlAttribute C = XmlUnit.FindAttributeOrCreateNew(node, "CTYPE");
            if (settingItem.getCtype() == NprojectType.CType.C)
            {
                C.Value = "C";
            }
            else
            {
                C.Value = "CPP";
            }

            XmlNode isjs = XmlUnit.FindNodeOrCreateNew(node, "ISJS");
            XmlAttribute isjsatt = XmlUnit.FindAttributeOrCreateNew(isjs, "Value");
            isjsatt.Value = settingItem.GetIsJS().ToString();

            XmlNode JS = XmlUnit.FindNodeOrCreateNew(node, "JS");
            JS.InnerText = settingItem.GetJS();

            XmlNode VB = XmlUnit.FindNodeOrCreateNew(node, "VB");
            VB.InnerText = settingItem.GetVB();


            XmlAttribute P = XmlUnit.FindAttributeOrCreateNew(node, "PTYPE");
            if (settingItem.getProjecttype() == NprojectType.PType.动态库)
            {
                P.Value = "share";
            }
            if (settingItem.getProjecttype() == NprojectType.PType.静态库)
            {
                P.Value = "static";
            }
            if (settingItem.getProjecttype() == NprojectType.PType.应用程序)
            {
                P.Value = "application";
            }

            SourceSetting ss = settingItem.GetSourceSetting();
            XmlNode snode = XmlUnit.FindNodeOrCreateNew(node, "Source");
            foreach (FileSetting file in ss.GetItem())
            {
                XmlNode filenode = XmlUnit.FindOrCreateNewNodeWithValue(snode, "File", "value", file.GetFile());
            }

            HeadSetting hs = settingItem.GetHeadSetting();
            XmlNode hnode = XmlUnit.FindNodeOrCreateNew(node, "Header");
            foreach (FileSetting file in hs.GetItem())
            {
                XmlNode filenode = XmlUnit.FindOrCreateNewNodeWithValue(hnode, "File", "value", file.GetFile());
            }
            XmlNode dependD = XmlUnit.FindNodeOrCreateNew(node, "DependDirectory");
            List<string> ddir = settingItem.GetDependHeaderDirectory();

            foreach (string ddirItem in ddir)
            {
                XmlNode ddnode = XmlUnit.FindOrCreateNewNodeWithValue(dependD, "File", "Name", ddirItem);

            }
            XmlNode dependL = XmlUnit.FindNodeOrCreateNew(node, "DependLibsDirectory");
            List<string> ddirL = settingItem.GetDependLibsDirectory();

            foreach (string ddirItem in ddirL)
            {
                XmlNode ddnode = XmlUnit.FindOrCreateNewNodeWithValue(dependL, "File", "Name", ddirItem);

            }


            Hashtable flags = settingItem.GetFlags();
            XmlNode flagsnode = XmlUnit.FindNodeOrCreateNew(node, "UFlags");
            flagsnode.RemoveAll();
            foreach (string KeyItem in flags.Keys)
            {
                XmlNode nodeItem = AddChild(flagsnode, "Item", "value", KeyItem);
                nodeItem.InnerText = flags[KeyItem].ToString();
            }
            Hashtable libs = settingItem.GetLibs();
            XmlNode libsnode = XmlUnit.FindNodeOrCreateNew(node, "ULibs");

            libsnode.RemoveAll();
            foreach (string KeyItem in libs.Keys)
            {
                XmlNode nodeItem = AddChild(libsnode, "Item", "value", KeyItem);
                nodeItem.InnerText = libs[KeyItem].ToString();
            }
        }

        private XmlNode AddChild(XmlNode flagsnode, string Name, string Att, string Key)
        {
            XmlNode node = flagsnode.OwnerDocument.CreateNode(XmlNodeType.Element, Name, "");
            XmlAttribute att = XmlUnit.FindAttributeOrCreateNew(node, Att);
            return node;
        }




    }
}
